package co.yixiang.retail.biz.user.mapper;


import co.yixiang.common.mproot.common.mapper.CoreMapper;

import co.yixiang.retail.common.domain.YxSystemStore;
import co.yixiang.retail.common.domain.YxUser;
import co.yixiang.retail.common.dto.RyzUseIntegralDetailDto;
import co.yixiang.retail.common.dto.YxBalanceChangeDto;
import co.yixiang.retail.common.dto.YxSystemStoreQueryCriteria;
import co.yixiang.upms.common.system.domain.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface YxSystemStoreMapper extends CoreMapper<YxSystemStore> {

    @Select("SELECT y.uid,y.nickname,y.real_name,y.avatar,y.now_money,y.brokerage_price,y.status,s.user_id,s.phone,s.id,s.address,s.retail_type,s.permit,s.audit_status,s.rejectcause,s.retail_user,s.is_disable from  yx_system_store s left join yx_user y on y.uid = s.user_id where s.phone=#{phone}")
    List<YxSystemStore> selectListByPhone(String phone);

    @Select("<script> " +
            "SELECT id,name,phone,address,detailed_address,image,retail_user from yx_system_store " +
            "where retail_type=0 and audit_status=1 " +
            "<if test=\"predicate.username != null and predicate.username!='' \">" +
            " and retail_user like \"%\"#{predicate.username}\"%\"  " +
            "</if>" +
            "<if test=\"predicate.phone !=null and predicate.phone !=''\">" +
            " and phone like \"%\"#{predicate.phone}\"%\"" +
            "</if><if test=\"predicate.address !=null and predicate.address !=''\">and address like \"%\"#{predicate.address}\"%\"</if> </script>")
    List<YxSystemStore> queryByType(@Param("predicate") YxSystemStoreQueryCriteria predicate);

    @Select("select y.real_name,y.integral, y.uid,y.store_balance,y.brokerage_price, s.id,s.name,s.phone,s.address,s.id,s.retail_type, s.permit,s.image,s.is_disable from yx_user y left join yx_system_store s on y.uid = s.user_id  where s.id  = #{id}")
    List<YxSystemStore> queryBalace(@Param("id") Integer id);
    @Select("select s.id,s.image,s.store_name,s.price,s.stock,r.store_id,r.product_id from yx_store_product s LEFT JOIN yx_store_product_stock_value r on s.id = r.product_id where r.store_id = #{id}")
    List<YxSystemStore> queryGoods(@Param("id")Integer id);

    @Select("SELECT uid,real_name,addres,avatar,nickname,phone,integral from yx_user WHERE uid = #{uid}")
    YxUser getUserById(@Param("uid") Long uid);

    @Update("update yx_user set integral =  #{integral} where uid = #{uid}")
    void updateIntegral(@Param("uid") Long uid, @Param("integral") BigDecimal integral);

    @Update("update yx_user set now_money =  #{money} where uid = #{uid}")
    void updateNowMoney(@Param("uid") Long uid, @Param("money") BigDecimal money);
    /**
     * 根据用户名查询用户信息
     * @param phone 用户名
     */
    @Select("SELECT u.uid id,u.nickname nick_name,u.status enabled,u.create_time,u.phone,u.`password`,u.username,u.avatar FROM `yx_user`  u WHERE u.phone = #{phone}")
    User login(String phone);
    @Select("SELECT uid,password,nickname,status enabled,create_time,phone,username,avatar,now_money,brokerage_price,integral FROM `yx_user`  WHERE uid=#{uid}")
    YxUser findYxUserById(@Param("uid") Long uid);


    @Update("update `yx_user` set password = #{password}  where uid = #{uid}")
    void updatePass(@Param("password") String encryptPassword,@Param("uid") Long uid);


    @Insert("insert into ryz_use_integral_detail (user_id,order_id,store_id,create_time,before_using_integral,using_integral,after_using_integral,phone) values (#{useIntegralDetail.userId},#{useIntegralDetail.orderId},#{useIntegralDetail.storeId},now(),#{useIntegralDetail.beforeUsingIntegral},#{useIntegralDetail.usingIntegral},#{useIntegralDetail.afterUsingIntegral},#{useIntegralDetail.phone})")
    void insertIntegralDetail(@Param("useIntegralDetail") RyzUseIntegralDetailDto ryzUseIntegralDetailDto);

    @Update("update yx_user set store_balance = #{nowMoney} where uid = #{uid}")
    void updateMoney(@Param("nowMoney") BigDecimal nowMoney,@Param("uid") Long uid);

    /**记录余额变动*/
    @Insert("insert into yx_balance_change (uid,change_turn,user_front,change_put,user_time,type) values (#{yxBalanceChange.uid},#{yxBalanceChange.changeTurn},#{yxBalanceChange.userFront},#{yxBalanceChange.changePut},now(),#{yxBalanceChange.type})")
    void insertBlanceChange(@Param("yxBalanceChange") YxBalanceChangeDto yxBalanceChangeDto);

    @Select("SELECT * from yx_user where (SELECT user_id from yx_system_store where id=#{storeId})=uid")
    YxUser getStoreUserById(@Param("storeId") Integer storeId);

    //根据门店id计算服务费
    @Select("SELECT service from yx_store_type where(select retail_type from yx_system_store where id=#{storeId})=level")
    BigDecimal findServiceCharge(@Param("storeId")Integer storeId);

    @Select("select yx_system_store.id " +
            "from yx_system_store " +
            "WHERE yx_system_store.user_id = #{uid}")
    Integer queryByUserId(@Param("uid") Integer uid);

    @Select("select retail_type as retailType from yx_system_store where id = #{storeId}")
    Integer queryById(@Param("storeId")Integer storeId);

    @Select("select is_disable from yx_system_store where id=#{id}")
    Boolean selectRecharge(@Param("id") Integer id);

    @Update("update  yx_user set store_balance = store_balance- #{extractPrice} where uid = #{uid}")
    void updateBanlance(@Param("uid") Long uid, @Param("extractPrice") BigDecimal extractPrice);
    @Select("select store_balance from yx_user where uid = #{uid}")
    BigDecimal queryStoreBalance(Long uid);
}
